//
//  JXLWelComeViewController.swift
//  MicroBlog
//
//  Created by 贾晓磊 on 16/9/24.
//  Copyright © 2016年 贾晓磊. All rights reserved.
//

import UIKit
import SDWebImage

class JXLWelComeViewController: UIViewController {

    //懒加载子控件
    //背景视图
  private lazy var backGroundImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //用户头像视图
    private lazy var userImageView: UIImageView = {
        
        let view = UIImageView(image: UIImage(named: "avatar_default_big"))

        //如果存储的用户数据中的图片地址不为空，就加载图片
        if let imageUrl = JXLUserAccountViewModel.sharedUserAccountViewModel.userAccount?.avatar_large {
        view.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        view.layer.cornerRadius = 85 / 2
        view.layer.masksToBounds = true
        return view
    }()
    
    //欢迎的label
    private lazy var messageLabel: UILabel = {
       let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 15)
        label.textColor = UIColor.darkGray
        
        if let name = JXLUserAccountViewModel.sharedUserAccountViewModel.userAccount?.name {
            label.text = "\(name),欢迎回来"
        } else {
             label.text = "欢迎回来"
        }
        
       
        label.alpha = 0
        return label
    }()
    

    override func loadView() {
        view = backGroundImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }

    //添加控件，设置约束
    private func setupUI() {
       
        view.addSubview(userImageView)
        view.addSubview(messageLabel)
        
        //设置userImageView的约束
        userImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSize(width: 85, height: 85))

        }
        //设置messageLabel的约束
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(userImageView)
            make.top.equalTo(userImageView.snp_bottom).offset(10)
        }

        
    }
    
    //动画效果
   private func startAnimation() {
    //修改用户头像的位置
    userImageView.snp_updateConstraints { (make) in
            make.top.equalTo(view).offset(100)
        }
    
    //  执行约束动画
    //  usingSpringWithDamping 阻尼 0 - 1, 阻尼越大弹簧效果越小
    //  initialSpringVelocity 初始速度
    UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { 
        
        //重新布局界面
        self.view.layoutIfNeeded()
        }) { (_) in
            
            //动画修改messageLabel的透明度
            UIView.animate(withDuration: 1, animations: {
                self.messageLabel.alpha = 1
                }, completion: { (_) in
                    //  完成切换界面到首页
                    NotificationCenter.default.post(name: Notification.Name(SwitchRootVCNotification), object: nil)
            })

    }
    
    
    }
   
}
